// 手写发布订阅
class EventHub {
    constructor() {
        this.cache = {}
    }
    on = function (eventName,fn) {
        this.cache[eventName] = this.cache[eventName]||[]
        this.cache[eventName].push(fn)
    }
    emit = function (eventName,pram) {
        if(!this.cache[eventName]) return false
        this.cache[eventName].forEach(function(fn) {
            fn(pram)
        })
    }
    off = function (eventName, fn) {
　　　　let index = this.cache[eventName].indexOf(fn)
　　　　if(index === -1) return
       this.cache[eventName].splice(index, 1)
    }
}

let p1 = new EventHub();
p1.on('detail', name => console.log(name));
p1.emit('detail', 'observer')
let p2 = new EventHub();
p2.on('detail', name => console.log(name));
p2.emit('detail', 'observer2')
p2.off('detail');
p2.emit('detail', 'observer3');